define(["app", 
        "jquery",
        "md5","ecb",
        "resources/UserResource", 
        "services/MenuService",
        "services/UserService"
], function(app) {
	
	var deps = ["$scope", "UserResource", "$location", "UserService", "MenuService"];
	
	function controller($scope, UserResource, $location, UserService,MenuService) {
		
		// 不记住密码
		$scope.pswFlag=0;
		
		var user =UserService.getCurrentUser();
		$scope.loginVo={};
		if(user==null){
			var rememberFlag =UserService.getCookieValue("rememberFlag");
			if(rememberFlag==1){
				var userName =UserService.getCookieValue("userName");
				var password =UserService.getCookieValue("password");
				$scope.loginVo.userName=userName;
				$scope.loginVo.password=password;
				$scope.pswFlag=1;
				$("#checkId").attr("checked", true);
			}
			else{
				UserService.deleteCookie("userName","/");
				UserService.deleteCookie("password","/");
				UserService.deleteCookie("rememberFlag","/");
				$scope.pswFlag=0;
				$("#checkId").attr("checked", false);
			}
			
		}
		else{
			
			if(user.pswFlag==1){
				$scope.loginVo.userName=user.loginName;
				$scope.loginVo.password=user.password;
				$("#checkId").attr("checked", true);
				$scope.pswFlag=1;
			}
			else
			{
				$("#checkId").attr("checked", false);
				$scope.loginVo.password=null;
				$scope.loginVo.userName=user.loginName;
				$scope.pswFlag=0;
			}
		}
		
		
		/**
		 * 是否记住密码
		 */
		$scope.remberPass=function(){
			var val=$("input[type='checkbox']").is(':checked')
			if(val==true){
				$scope.pswFlag=1;
			}
			else {
				$scope.pswFlag=0;
			}
		};
		
		
		/**
		 * 监听回车键，直接登陆
		 */
		$scope.enterSearch = function($event){
            if ($event.keyCode === 13) {
                $scope.login();
            }
        };
        
        $scope.isPoneAvailable =function (){
        	 var mobile =$scope.companyVo.mobile;
        	 var myreg=/^[1][3,4,5,7,8,9][0-9]{9}$/;
             if (!myreg.test(mobile)) {
                 return false;
             } else {
                 return true;
             }
        }
        
		/**
		 * 用户登陆
		 */
		$scope.login =function(){
			UserResource.login({
				userName : $scope.loginVo.userName,
				password : encrypt($scope.loginVo.password, $scope.loginVo.userName)
			}, function(user) {
				if(user.authenticated) {
					var pswFlag =$scope.pswFlag;
					user.pswFlag=pswFlag;
					// 记住密码
					UserService.setCookie("password",$scope.loginVo.password,24,"/");
					user.password=$scope.loginVo.password;
					UserService.setCookie("userName",$scope.loginVo.userName,24,"/");
					if(pswFlag==1){
						UserService.setCookie("rememberFlag",$scope.pswFlag,24,"/");
					}else{
						UserService.deleteCookie("rememberFlag","/");
					}
					UserService.setCurrentUser(user);
					// 超级管理员
					if(user.userType==1){
						$scope.menuConfig = MenuService.adminConfig;
					}
					// 后台管理员
					if(user.userType==2){
						$scope.menuConfig = MenuService.adminConfig;
					}
					if(user.menus && user.menus.length > 0){
						var url = "", 
						mymenu = user.menus[0];
						if(mymenu.subMenus && mymenu.subMenus.length > 0){
							url = $scope.menuConfig[mymenu.subMenus[0].markName].locationURL;
						} else {
							url = $scope.menuConfig[mymenu.markName].locationURL;
						}
						// 普通管理员
						if(user.userType==1){
							$location.path(url);
						}
						// 超级管理员
						if(user.userType==2){
							$location.path(url);
						}
						
					}
				} else {
					$scope.errorMessage = user.errorMessage;
					UserService.removeCurrentUser();
				}
			});
		};
		
		/** 监听用户名是否为空 **/
		$scope.$watch('loginVo.userName', function(value) {
			if($scope.errorMessage){
				if(value==null){
	            	$scope.errorMessage=false;
	            }
			}
        });
		/** 监听密码是否为空 **/
		$scope.$watch('loginVo.password', function(value) {
			if($scope.errorMessage){
				if(value==null){
	            	$scope.errorMessage=false;
	            }
			}
        });
	}
	
	/**
	 * 密码 encrypt 加密
	 */
	function encrypt(content,srcKey){
	      var keyLen = 8;
		  var srcKeyLen = srcKey.length;
		
		 if(srcKeyLen > keyLen){
			 srcKey = srcKey.substr(0,keyLen);
			
		 }else{
			 var paddLen = keyLen -  srcKeyLen;
			 for(var i=0;i<paddLen;i++){
				 srcKey += "0";
			 }
		 }
		
		 var key = CryptoJS.enc.Utf8.parse(srcKey);		
		 var encryptStr =  CryptoJS.DES.encrypt(content, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
		return encryptStr.toString();
	}
	controller.$inject = deps;
	app.lazy.controller("LoginController", controller);
});
